﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CommandController
{
    static CommandController instance_;
    public static CommandController instance
    {
        get
        {
            if (instance_ == null)
            {
                instance_ = new CommandController();
            }
            return instance_;
        }
    }

    public void Update ()
    {
        PickCommand();
    }

    Vector3 downPos;
    public void PickCommand()
    {
        if (Input.GetMouseButtonDown(0))
        {
            downPos = Input.mousePosition;
        }
        if (Input.GetMouseButtonUp(0))
        {
            Vector3 curPos = Input.mousePosition;
            Vector3 delta = curPos - downPos;
            if (delta.magnitude > 50)
            {
                if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
                {
                    // left or right
                    if (delta.x > 0)
                    {
                        //right
                        TrigerEvent(CommandType.Right);
                    }
                    else
                    {
                        TrigerEvent(CommandType.Left);
                    }
                }
                else
                {
                    //up or down
                    if (delta.y > 0)
                    {
                        TrigerEvent(CommandType.Up);

                    }
                    else
                    {
                        TrigerEvent(CommandType.Down);
                    }
                }
            }
        }
    }

    public List<CommandType> cammandCache = new List<CommandType>();
    Action<CommandType> OnTrigerCommand;
    public void RegCommandEvent(Action<CommandType> eventCB)
    {
        OnTrigerCommand = eventCB;
    }

    public void UnRegCommandEvent()
    {
        OnTrigerCommand = null;
    }

    void TrigerEvent(CommandType ct)
    {
        if (OnTrigerCommand != null)
        {
            OnTrigerCommand(ct);
        }
    }

    public List<CommandType> GetRandomCommand(int count=1000)
    {
        List<CommandType> ctLst = new List<CommandType>();
        for (int i = 0; i < count; i++)
        {
            ctLst.Add((CommandType)UnityEngine.Random.Range(0,4));
        }
        return ctLst;
    }

}

public enum CommandType
{
    Up,
    Down,
    Left,
    Right,
    None
}
